package ee.pri.pink.kallur.gl;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import ee.pri.pink.kallur.gl.objects.GLCube;
import ee.pri.pink.kallur.gl.objects.GLWorld;
import ee.pri.pink.kallur.gl.objects.Level;

import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;

public class KallurRenderer implements Renderer {
	
	GLWorld world;
	
	GLCube cube;
	
	private float angle = 0.0f;
	
	public KallurRenderer() {
		world = new GLWorld();
		cube = new GLCube();
		Level.load("test", world);
	}
	
//	private ShortBuffer _indexBuffer;
//	private FloatBuffer _vertexBuffer;
//	private short[] _indicesArray = {0, 1, 2};
//	private int _nrOfVertices = 3;

	@Override
	public void onDrawFrame(GL10 gl) {
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		
		// reset the transformation matrix
		

		
		
//		gl.glTranslatef(0.0f, 0.0f, -5.0f);
		
		gl.glLoadIdentity();
		
//		angle += 1.0f;
		
		gl.glTranslatef(0.0f, 0.0f, 0.0f);
//		gl.glRotatef(angle, 0.0f, 0.0f, 1.0f);
//		gl.glTranslatef(-11.5f, 0.0f, 0.0f);
		
//		gl.glLoadIdentity();
		
//		gl.glPushMatrix();
//		
//		cube.render(gl);
//		
//		gl.glTranslatef(2.0f, 2.0f, 0.0f);
//		
//		cube.render(gl);
//		
//		gl.glPopMatrix();
		
		world.render(gl);
		
//		gl.glRotatef(1.0f, 1f, 0f, 0f);
		
//		gl.glRotatef(rotation[0], 1.0f, 0.0f, 0.0f);
//		gl.glRotatef(rotation[1], 0.0f, 1.0f, 0.0f);
//		gl.glRotatef(rotation[2], 0.0f, 0.0f, 1.0f);
//		gl.glRotatef(rotation[0], rotation[1], rotation[2], 0.0f);
		
//		gl.glColor4f(0.5f, 0f, 0f, 0.5f);
		 
	    // define the vertices we want to draw
//	    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer);
	    
//	    gl.glDraw
	 
	    // finally draw the vertices
//	    gl.glDrawElements(GL10.GL_TRIANGLES, _nrOfVertices, GL10.GL_UNSIGNED_SHORT, _indexBuffer);
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		// Sets the current view port to the new size.
		gl.glViewport(0, 0, width, height);// OpenGL docs.
		// Select the projection matrix
		gl.glMatrixMode(GL10.GL_PROJECTION);// OpenGL docs.
		// Reset the projection matrix
		gl.glLoadIdentity();// OpenGL docs.
		// Calculate the aspect ratio of the window
		GLU.gluPerspective(gl, 45.0f,
                                   (float) width / (float) height,
                                   0.1f, 100.0f);
		// Select the modelview matrix
		gl.glMatrixMode(GL10.GL_MODELVIEW);// OpenGL docs.
		// Reset the modelview matrix
		gl.glLoadIdentity();// OpenGL docs.

	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
		
	    gl.glEnable(GL10.GL_CULL_FACE);
	    gl.glFrontFace(GL10.GL_CW);
	    gl.glCullFace(GL10.GL_BACK);
	    
		gl.glShadeModel(GL10.GL_SMOOTH);
		// Depth buffer setup.
		gl.glClearDepthf(1.0f);
		// Enables depth testing.
		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glDepthFunc(GL10.GL_LEQUAL);
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);

	}

}
